//
//  ShareListImageView.m
//  
//
//  Created by 景 on 15/10/4.
//
//

#import "ShareListImageView.h"
#import "UIImageView+WebCache.h"
#import "UIView+Helper.h"
//图片的宽高
//图片之间的间距
#define ShareListImageMargin 10
//图片最大的行数(注意：主要是为了使4张图片时也显示为田字格)
#define ShareListImageMaxCol(count) ((count==4)?2:3)

@interface ShareListImageView () {
    
    CGFloat ShareListImageWH;
}

@property(nonatomic, strong) NSMutableArray *array;

@end

@implementation ShareListImageView// 9

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

- (void)setImages:(NSArray *)images
{
    _images = images;
    NSUInteger imagesCount = images.count;
    
    //创建足够数量的图片控件
    //这里的self.subviews.count不要单独赋值给其他变量
    while (self.subviews.count < imagesCount) {
        
        UIImageView *imageView = [[UIImageView alloc] init];
      

//        [imageView setContentScaleFactor:[[UIScreen mainScreen] scale]];
//        imageView.contentMode =  UIViewContentModeScaleAspectFill;
//        imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
//        imageView.clipsToBounds  = YES;
        
        
        
        
        
        [imageView setClipsToBounds:YES];
        imageView.userInteractionEnabled = YES;

        //添加手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
        tap.numberOfTapsRequired = 1;
        tap.numberOfTouchesRequired = 1;
        [tap addTarget:self action:@selector(imagesTapped:)];
        imageView.userInteractionEnabled = YES;
        imageView.tag = self.subviews.count + 200;
        [imageView addGestureRecognizer:tap];
        
        
        [self addSubview:imageView];
    }
    
    if (imagesCount == 1) {
        
        ShareListImageWH = 180;
        
        
    } else {
        
        ShareListImageWH = (ViewWidth - 10 * 4) / 3;
    }
    
    //遍历所有的图片控件，设置图片
    _array = [[NSMutableArray alloc] init];
    for (int i = 0; i < self.subviews.count; i++) {
        UIImageView *imageView = self.subviews[i];
        
        if (i < imagesCount) {//显示
            imageView.hidden = NO;
            
            //设置图片
//            NSString *str = image.blogsimage_name;
            [imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.images[i]]] placeholderImage:[UIImage imageNamed:@"backImage"]];
//            imageView.image = [UIImage imageNamed:@"经销商（绿色）"];
            NSString *name = self.images[i];
            
            [_array addObject:name];
            
        } else{//隐藏
            imageView.hidden = YES;
        }
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    //设置图片的尺寸和位置
    NSUInteger imageCount = self.images.count;
    int maxCol = ShareListImageMaxCol(imageCount);
    for (int i = 0; i < imageCount; i++) {
        UIImageView *imageView = self.subviews[i];
        
        int col = i % maxCol;
        imageView.x = col * (ShareListImageWH + ShareListImageMargin);
        
        int row = i / maxCol;
        imageView.y = row * (ShareListImageWH + ShareListImageMargin);
        imageView.width = ShareListImageWH;
        imageView.height = ShareListImageWH;
    }
}

- (CGSize)sizeWithCount:(int)count {
    
    //最大列数(一行最多有多少列)
    int maxCols = ShareListImageMaxCol(count);
    
    //行数
    int cols = (count >= maxCols)? maxCols : count;
    CGFloat imageW = cols * ShareListImageWH + (cols - 1) *ShareListImageMargin;
    
    //列数
    int rows = (count + maxCols - 1) / maxCols;
    CGFloat imageH = rows * ShareListImageWH + (rows - 1) * ShareListImageMargin;
    
    return CGSizeMake(imageW, imageH);
}

- (void)imagesTapped:(UITapGestureRecognizer *)gesture {
    
    UIImageView *imageView = (UIImageView *)gesture.view;
    
    int index = (int)imageView.tag;
    
    if ([self.imagesDelegate respondsToSelector:@selector(imageWithName:andIndex:)]) {
        [self.imagesDelegate imageWithName:self.array andIndex:index];
    }
}

@end
